<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Moontoast\Math\BigNumber;

class InstallmentItem extends Model
{
    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_PROCESSING = 'repaying';
    const REFUND_STATUS_SUCCESS = 'finished';
    const REFUND_STATUS_FAILED = 'failed';

    public static $refundStatusMap = [
        self::REFUND_STATUS_PENDING => '未退款',
        self::REFUND_STATUS_PROCESSING => '退款中',
        self::REFUND_STATUS_SUCCESS => '退款成功',
        self::REFUND_STATUS_FAILED => '退款失败',
    ];

    /**
     * 可以被批量赋值的属性
     *
     * @var array
     */
    protected $fillable = [
        'sequence',
        'base',
        'fee',
        'fine',
        'due_date',
        'paid_at',
        'payment_method',
        'payment_no',
        'refund_status',
    ];

    /**
     * 应该转换为日期格式的属性
     *
     * end_at 会自动转为 Carbon 类型
     * @var array
     */
    protected $dates = ['due_date', 'paid_at'];

    /**
     * 获取分期
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function installment()
    {
        return $this->belongsTo(Installment::class);
    }

    /**
     * 创建一个访问器，返回当前还款计划需要还款的总金额
     *
     * @return string
     */
    public function getTotalAttribute()
    {
        $total = big_number($this->base)->plus($this->fee);
        if (!is_null($this->fine)) {
            $total->plus($this->fine);
        }

        return $total;
    }

    /**
     * 创建一个访问器，返回当前还款计划是否已经逾期
     *
     * @return bool
     */
    public function getIsOverdueAttribute()
    {
        return Carbon::now()->gt($this->due_date);
    }
}
